﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.Filter
{
    using Controls;
    using Helper;    
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.Windows.Forms;

    /// <summary>
    /// Filter to get sketched figures into a bitmap.
    /// </summary>
    /// 
    public class ImageSketch : AbstractFilter
    {
        private readonly List<Figure> figures;

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageSketch"/> class.
        /// </summary>
        public ImageSketch(List<Figure> figures)
        {
            SupportedSrcPixelFormats = PixelFormatFlags.Format24BppRgb;
            this.figures = figures;
        }

        /// <summary>
        /// Extracts the color segments of the original bitmap as pixel-connected regions.
        /// </summary>
        protected override unsafe void Process(BitmapData srcData, BitmapData dstData)
        {
            // int ps = Image.GetPixelFormatSize(srcData.PixelFormat) / 8;
            const int ps = 3;
            int w = srcData.Width;
            int h = srcData.Height;
            int offset = srcData.Stride - w * ps;

            int percent = 0;
             ProgressBarForm form = 
                 new ProgressBarForm
                 {
                     ProgressBar = { Maximum = 100 },
                     StartPosition = FormStartPosition.CenterScreen
                 };
             form.Show();
             form.TopMost = true;

            byte* src = (byte*)srcData.Scan0.ToPointer();
            byte* dst = (byte*)dstData.Scan0.ToPointer();
            // for each line
            for (int y = 0; y < h; y++)
            {
                // for each pixel
                for (int x = 0; x < w; x++, src += ps, dst += ps)
                {
                    bool isPart = false;
                    foreach (Figure fig in figures)
                    {
                        if (fig.Contains(x, y))
                        {
                            isPart = true;
                        }
                    }

                    if (isPart)
                    {
                        dst[RGBA.R] = src[RGBA.R];
                        dst[RGBA.G] = src[RGBA.G];
                        dst[RGBA.B] = src[RGBA.B];
                    }
                    else
                    {
                        dst[RGBA.R] = 255;
                        dst[RGBA.G] = 255;
                        dst[RGBA.B] = 255;
                    }
                }
                src += offset;
                dst += offset;

                int newPercent = y * 100 / h;
                form.Text = newPercent + "%";
                form.Resultlabel.Text = "Already processed: " + newPercent + "%";
                if (percent != newPercent)
                {
                    percent = newPercent;
                    form.ProgressBar.PerformStep();
                    form.Refresh();
                }
            }

            form.Dispose();
        }
    }
}
